home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 1.1 KB | 52 lines | [TEXT/CWIE] |
- // FinitePoolBase.cp
-
- #ifndef FinitePoolBase_h
- #include "FinitePoolBase.h"
- #endif
-
- FinitePoolBase::FinitePoolBase( void *theSpace,
- BitArrayBase& theMap,
- uint32 theElementSize )
- : space( static_cast<uint8 *>(theSpace) ),
- map( theMap ),
- elementSize( theElementSize ),
- allocated( 0 ),
- nextAllocation( 0 )
- {
- Assert( CanMultiply( theMap.Length(), theElementSize ) )
- map.ClearAll();
- }
-
- void *FinitePoolBase::Allocate( uint32 size )
- {
- Assert( size == elementSize );
- Assert( !Full() );
-
- uint32 slot = map.FirstZero( nextAllocation );
- if ( slot >= map.Length() )
- slot = map.FirstZero();
-
- Assert( slot < map.Length() );
- nextAllocation = slot + 1;
-
- map[ slot ].Set();
- allocated++;
- return space + size * slot;
- }
-
- void FinitePoolBase::Release( void *vp )
- {
- uint8 *const p = static_cast< uint8 * >( vp );
-
- Assert( p >= space );
- Assert( p - space < map.Length() * elementSize );
- Assert( (p - space) % elementSize == 0 );
- Assert( !IsEmpty() );
-
- uint32 slot = (p - space) / elementSize;
- Assert( map[ slot ] );
-
- map[ slot ].Clear();
- allocated--;
- }
-